#include "../include/Configuration.h"

//初始化静态成员变量
Configuration* Configuration::_pConfInstance = nullptr;

const static string confPath = "../conf/ConfFile.conf";

Configuration::Configuration()
{
    storeConfToMap();
}

Configuration* Configuration::getInstance()
{
    if(_pConfInstance==nullptr)
    {
        _pConfInstance = new Configuration();
        cout << "Configuration" << endl;
    }
    return _pConfInstance;
}

void Configuration::showMap()
{
    for(auto &conf:_confMap)
    {
        cout << conf.first << " " << conf.second << endl;
    }
}

void Configuration::storeConfToMap()
{
    ifstream ifs(confPath);
    if(!ifs.good())
    {
        perror("Configuration::storeConfToMap open file failed!");
        return;
    }

    string line;
    while (getline(ifs,line))
    {
        //逐行读取配置，如port 8888
        //文件流方式去除空格
        std::stringstream iss(line);
        string key;
        string value;
        if(iss>>key>>value)
        {
            _confMap[key] = value;
        }
    }
}

map<string,string>& Configuration::getConfMap()
{
    return _confMap;
}